package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.search.domian.DestinationEs;
import cn.wolfcode.luowowo.search.domian.StrategyEs;
import cn.wolfcode.luowowo.search.domian.TravelEs;
import cn.wolfcode.luowowo.search.domian.UserInfoEs;
import cn.wolfcode.luowowo.search.service.IDestinationEsService;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.service.ITravelEsService;
import cn.wolfcode.luowowo.search.service.IUserInfoEsService;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

// search 初始化
@RestController
public class DataController {



    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;


    @Autowired
    private IUserInfoEsService userInfoEsService;
    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private IDestinationEsService destinationEsService;
    @Autowired
    private ITravelEsService travelEsService;



    @GetMapping("/dataInit")
    public Object dataInit(){
        // 拿到所有的userInfo对象集合
        List<UserInfo> UserInfoList = userInfoService.listAll();
        for (UserInfo userInfo : UserInfoList) {
            // 遍历得到每一个UserInfo对象
            UserInfoEs userInfoEs = new UserInfoEs();
            // copy 属性
            BeanUtils.copyProperties(userInfo,userInfoEs);
            //执行插入方法
            userInfoEsService.save(userInfoEs);
        }

        // 攻略的
        List<Strategy> StrategyList = strategyService.list();
        for (Strategy strategy : StrategyList) {
            // 遍历得到每一个UserInfo对象
            StrategyEs strategyEs = new StrategyEs();
            // copy 属性
            BeanUtils.copyProperties(strategy,strategyEs);
            //执行插入方法
            strategyEsService.save(strategyEs);
        }

        // 目的地的
        List<Destination> destinationList = destinationService.list();
        for (Destination destination : destinationList) {
            DestinationEs destinationEs = new DestinationEs();
            BeanUtils.copyProperties(destination,destinationEs);
            destinationEsService.save(destinationEs);
        }


        // 游记的(记得要通过travel对象的userId属性找到UserInfo对象 set回 travel对象的Author 属性中)
        List<Travel> travelList = travelService.list();
        for (Travel travel : travelList) {
            TravelEs travelEs = new TravelEs();
            BeanUtils.copyProperties(travel,travelEs);
            travelEsService.save(travelEs);
        }

        return "yes";
    }


}
